//验证输入框输入的值

let checkApi = {
    upc(value) {
        //限制upc最多13位整数
        value = value.replace(/\D/g, '').substring(0, 13)
        return value
    },
    itemName(value) {
        //限制商品名称 和 分类名称 商品规格最多20位
        value = value.substring(0, 20)
        value = value.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
        var regStr = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig;
        value=value.replace(regStr,'')
        return value
    },
    retrieval(value) {
        //检索值不能输入图片字符
        value = value.replace(/[^a-zA-Z0-9\u4e00-\u9fa5]/g, '')
        value = value.substring(0, 20)
        return value
    },
    price(value) {
        //进货价格 销售价格 进货数量
        value = value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        value = value.replace(/^\.$/, "");     //如果以.开头或结尾就替换
        value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
        if (value != "") {
            let index = value.indexOf('.');
            if (index > 0) {
                let n=0
                if(index>=5){
                    n=5
                }else{
                    n=index
                }
                let a = value.substring(0, n)
                let b = value.substring(index)
                value=a+b
            }else{
                value = value.replace(/\D/g, '').substring(0, 5)
                value = parseFloat(value);

            }
        }
        return value
    },
    stockNumber(value) {
        // 散货进货数量
        value = value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        value = value.replace(/^\.$/, "");     //如果以.开头或结尾就替换
        value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        value = value.replace(/^(\-)*(\d+)\.(\d\d\d).*$/, '$1$2.$3');//只能输入两个小数
        // value = value.replace(/^(\-)*(\d+)\.+(\d\d\d).*$/, '$1$2.$3');//只能输入两个小数

        if (value != "") {
            let index = value.indexOf('.');
            if (index > 0) {
                let n=0
                if(index>=5){
                    n=5
                }else{
                    n=index
                }
                let a = value.substring(0, n)
                let b = value.substring(index)
                value=a+b
            }else{
                value = value.replace(/\D/g, '').substring(0, 5)
                value = parseFloat(value);

            }
        }
        return value

    },
    quantityCheck(value){
        //商品数量，限制输入整数
        value=String(value)
        value = value.replace(/\D/g, '').substring(0)
        if (value != "") {
            value = parseInt(value);
        }
        return value
    },
    qualityPeriod(value) {
        //保质期
        value = value.replace(/\D/g, '').substring(0, 4)
        if (value != "") {
            value = parseFloat(value);
        }
        return value
    },
    orderId(randId) {
        let test = /^([1-9][0-9]{0,24})$/.test(randId)
        if (!randId) {
            return true
        }
        return test
    },
    //XSS特殊字符转义
    escapeHTML: function(a) {
        if(!a) return "";
        a = a.replace(/>/g, "&gt;")
        a = a.replace(/</g, "&lt;")
        a = a.replace(/&/g, "&amp;")
        a = a.replace(/"/g, "&quot;")
        return a = a.replace(/'/g, "&#039;")
    },
    //XSS特殊字符反转义
    unescapeHTML: function(a) {
        if(!a) return "";
        a = a.replace(/&gt;/g, ">")
        a = a.replace(/&lt;/g, "<")
        a = a.replace(/&amp;/g, "&")
        a = a.replace(/&quot;/g, '"')
        return a = a.replace(/&#039;/g, "'")
    },
   //验证输入框输入的值


  isIDCard: function(value){
      //这里没有验证有效性，只验证了格式
      var a = new RegExp(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/),
          b = new RegExp(/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/);
      return a.test(value.trim()) || b.test(value.trim());
  },
  phoneSpace: function(value){
      return value.replace(/^(\d{3})(\d{4})(\d{4})$/, '$1 $2 $3');
  },
  isMobile(value){
    var isM=new RegExp( /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/);
     return isM.test(value.trim())
      // return   /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))|(19[0-9]{1}))+\d{8})$/.test(value)
  },
  name(value){
      value=value.replace(/[^\u4E00-\u9FA5]/g,'')
      return value.substring(0, 6)
  },
  address(value){
      value=value.replace(/[^0-9A-Za-z-_,^&（$）%，*()+!@#\u4E00-\u9FA5\uF900-\uFA2D]/g,'')
      return value.substring(0, 40)
  },
  exNum(value){
      value=value.replace(/[^0-9]+/g,'')
      console.log(value)
      return value
  },
  tel(value){
      value=value.replace(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,'')
      return value
  },
  code(value){
      value=value.replace(/[^0-9A-Za-z]+/g,'')
      return value
  },
}
export default checkApi;
